import Layout from '@/core/layout/index.vue'
import router from '@/core/router/index.ts'
import { HOME_URL } from '@/core/config/index.ts'

/**
 * 注意：使用console.log("路由数据", JSON.stringify(generateRoutes(res.data, 0))打印会发现子路由的componet打印不出来，JSON不能打印出来函数。${data[i].component}
 */
// 递归函数用于生成路由配置，登录的时候也需要调用一次。
export function generateRoutes(data: any[], parent_key: any) {
  // 首先把你需要动态路由的组件地址全部获取[vue2中可以直接用拼接的方式，但是vue3中必须用这种方式]
  let modules = import.meta.glob('@/app/views/**/*.vue')
  let addonModules = import.meta.glob('@/addon/**/views/**/*.vue')
  const routeList: any = []
  for (var i = 0; i < data.length; i++) {
    if (data[i] && !router.hasRoute(data[i].router_path)) {
      if (data[i].parent_key === parent_key) {
        // console.log("component", data[i].component);
        const componentTemplate = data[i].view_path
        const route: any = {
          path: `/${data[i].router_path}`,
          name: `${data[i].key}`,
          // 这里modules[`/src/views/${componentTemplate}.vue`] 一定要用绝对定位
          // component: data[i]?.view_path
          //   ? modules[`/src/app/views/${componentTemplate}.vue`]
          //   : Layout,
          component: data[i].addon
            ? addonModules[
                `/src/addon/${data[i].addon}/views/${data[i].view_path}.vue`
              ]
            : modules[`/src/app/views/${data[i].view_path}.vue`],
          meta: {
            app_type: data[i].app_type,
            addon: data[i].app_type,
            title: data[i].name,
            icon: data[i].icon,
            isHide: data[i].is_hide,
            isKeepAlive: data[i].is_keep_alive,
            isLink: data[i].is_link,
            isFull: data[i].is_full,
            isAffix: data[i].is_affix,
          },
        }
        // console.log("component", route.component);
        if (data[i].type == '3') {
          route.redirect = `${data[i]?.redirect}` || HOME_URL
        }
        // 递归处理子节点
        const children = generateRoutes(data, data[i].key)
        if (children.length > 0) {
          route.children = children
        }

        routeList.push(route)
      }
    }
  }
  return routeList
}

/**
 * 初始化动态路由[用于生成扁平化一级路由，将后端一级路由数据转化为前端router格式的一级路由]
 */
export function generateFlattenRoutes(data: any[]) {
  // 首先把你需要动态路由的组件地址全部获取[vue2中可以直接用拼接的方式，但是vue3中必须用这种方式]
  let modules = import.meta.glob('@/app/views/**/*.vue')
  let addonModules = import.meta.glob('@/addon/**/views/**/*.vue')
  const routes: any = []
  for (var i = 0; i < data.length; i++) {
    // console.log("component",data[i].component)
    const componentTemplate = data[i].view_path
    const route: any = {
      path: `/${data[i].router_path}`,
      name: `${data[i].key}`,
      // 这里modules[`/src/views/${componentTemplate}.vue`] 一定要用绝对定位
      // component: data[i]?.view_path
      //   ? modules[`/src/app/views/${componentTemplate}.vue`]
      //   : Layout,
      component: data[i].addon
        ? addonModules[
            `/src/addon/${data[i].addon}/views/${data[i].view_path}.vue`
          ]
        : modules[`/src/app/views/${data[i].view_path}.vue`],
      meta: {
        addon: data[i].app_type,
        parent_key: data[i].parent_key,
        app_type: data[i].app_type,
        parentId: data[i].parent_key,
        title: data[i].name,
        icon: data[i].icon,
        isHide: data[i].is_hide,
        isKeepAlive: data[i].is_keep_alive,
        isLink: data[i].is_link,
        isFull: data[i].is_full,
        isAffix: data[i].is_affix,
      },
    }
    // console.log("component",route.component)
    if (data[i].type == '3') {
      route.redirect = `${data[i]?.redirect}` || HOME_URL
    }
    routes.push(route)
  }
  return routes
}
